﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DBTools.DBUpdate.Comm
{
    public class TableSqlHelper
    {
        public static string CreteaInitDBVersionSqlString(string DbName, string SystemName)
        {
            string _Sql = string.Format(@"
                            use {0}
                            if(not exists(select * from sysobjects where name='t_DBVersion'))
                                begin
                                CREATE TABLE t_DBVersion (
	                                s_KeyGuid varchar(50) NOT NULL ,
	                                s_DBVersion varchar(50) NOT NULL ,
	                                s_SystemName varchar(50) NULL ,
	                                d_InsertDate datetime NULL ,
	                                d_UpdateDate datetime NULL ,
	                                s_Mark varchar(100) NULL 
	                                )

                                ALTER TABLE t_DBVersion
	                                ADD CONSTRAINT XPKt_DBVersion PRIMARY KEY  CLUSTERED (s_KeyGuid ASC)
                                end
                              insert t_DBVersion(s_KeyGuid,s_DBVersion,s_SystemName,d_InsertDate,d_UpdateDate) values(newid(),'2001-01-01','{1}',getdate(),getdate())
                                ", DbName, SystemName);
            return _Sql;
        }
        public static string UpdateDBVersionSqlString(string DbName, string _LastVersion)
        {
            string _Sql = string.Format(@"use {0}
                                          update t_DBVersion set s_DBVersion='{1}' ", DbName, _LastVersion);
            return _Sql;
        }
        public static string GetDBVersionSqlString(string DbName)
        {
            string _Sql = string.Format(@"use {0}
                                          select s_DBVersion from  t_DBVersion ", DbName);
            return _Sql;
        }
    }
}
